//
//  YCLNewFeatureController.swift
//  新浪微博01
//
//  Created by yaochunlin on 16/4/12.
//  Copyright © 2016年 yaochunlin. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class YCLNewFeatureController: UICollectionViewController {

    
    //流布局属性
    let  flowLayout = UICollectionViewFlowLayout()
    
    //
    init(){
    
        super.init(collectionViewLayout: flowLayout)
        
        flowLayout.itemSize = UIScreen.mainScreen().bounds.size
        //设置水平滚动
        flowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //水平间距，垂直间距均为0
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        //设置分页
        collectionView?.pagingEnabled = true
        //取消弹簧效果
        collectionView?.bounces = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        self.collectionView!.registerClass(YCLNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }
    
    //MARK 数据源方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return NewFeatureItemCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! YCLNewFeatureCell
        
        cell.currentIndex = indexPath.item
        
//        if cell.currentIndex == NewFeatureItemCount - 1 {
//            
//            cell.startAnimation()
//        }
        
        return cell
        
    }
    
   // MARK scrollview代理方法
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let index = Int(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.size.width)
        
        if index == NewFeatureItemCount - 1 {
            
            
            //获得最后一个cell，让他开启动画
            let  indexPath = NSIndexPath(forItem: index,inSection: 0)
            
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! YCLNewFeatureCell
            
            cell.startAnimation()
        }
    
    }

}
